package cn.zlh.ht.serviceimpl;

import cn.zlh.ht.entity.Agreement;
import cn.zlh.ht.entity.Template;
import cn.zlh.ht.mapper.AgreementMapper;
import cn.zlh.ht.mapper.TemplateMapper;
import cn.zlh.ht.service.IAgreementService;
import cn.zlh.ht.service.TemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 合同模板 服务层接口实现
 * @author 张乐焕
 */
@Slf4j
@Service
@Transactional
public class TemplateServiceImpl implements TemplateService {
    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public void createTemplate(Template template) {
        templateMapper.createTemplate(template);
    }


    @Override
    public List<Map<String, Object>> getAllTemplateColumn() {
        // 定义存储所有表字段信息的 List
        List<Map<String, Object>> allTemplates = new ArrayList<>();

        // 获取所有模板表名和注释
        List<Map<String, String>> tableList = templateMapper.getAllTemplateTableName();

        // 遍历每个表，查询字段信息并存入 List
        for (Map<String, String> table : tableList) {
            String tableName = table.get("TABLE_NAME"); // 取出表名
            String tableComment = table.get("TABLE_COMMENT"); // 取出表注释

            List<Map<String, String>> fields = getFieldByTableName(tableName); // 查询字段信息

            // 组装数据
            Map<String, Object> tableInfo = new HashMap<>();
            tableInfo.put("TABLE_COMMENT", tableComment);
            tableInfo.put("TABLE_NAME", tableName);
            tableInfo.put("FIELDS", fields);

            allTemplates.add(tableInfo); // 存入 List
        }

        return allTemplates; // 返回所有表的字段信息
    }

    @Override
    public void insertData(Template template) {
        String tableName = template.getTableName();
        String agreementId = template.getAgreementId();
        Map<String, String> fields = template.getFields();
        templateMapper.insert(tableName, agreementId, fields);
    }

    @Override
    public Object getTemplateDataByAgreemetnId(String tableName, String agreementId) {
        String tableCN = templateMapper.getTemplateLabelCN(tableName);
        List<Map<String, Object>> fieldsCN = templateMapper.getTemplateFieldsLabelCN(tableName);
        Map<String, Object> data = templateMapper.getTemplateDataByAgreemetnId(tableName, agreementId);

        Map<String, Object> result = new LinkedHashMap<>();
        for (Map<String, Object> field : fieldsCN) {
            String columnName = (String) field.get("COLUMN_NAME");
            String columnComment = (String) field.get("COLUMN_COMMENT");
            if (!"id".equals(columnName) && !"agreement_id".equals(columnName) && data.containsKey(columnName)) {
                result.put(columnComment, data.get(columnName));
            }
        }

        result.put("tableCN", tableCN);

        return result;
    }


    private List<Map<String, String>> getFieldByTableName(String templateName) {
        // 获取查询结果
        return templateMapper.getFieldByTableName(templateName);
    }


}